<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>构造函数</title>
  </head>
  <body>
    <script>
      // ES5 实现继承
      function Phone(brand, price) {
        this.brand = brand;
        this.price = price;
      }
      // 将方法挂载在原型链上，便于方法的复用
      Phone.prototype.call = function () {
        console.log("可以打电话");
      };

      // 子构造函数
      function SmartPhone(brand, price, color, size) {
        Phone.call(this, brand, price); // 改变Phone中this的指向,指向SmartPhone的实例化对象
        this.color = color;
        this.size = size;
      }
      // 设置子类构造函数的原型
      SmartPhone.prototype = new Phone();
      SmartPhone.prototype.constructor = SmartPhone;
      // 声明子类方法
      SmartPhone.prototype.photo = function () {
        console.log("我可以拍照");
      };
      SmartPhone.prototype.playgame = function () {
        console.log("可以玩游戏");
      };
      const xiaomi = new SmartPhone("小米", 1999, "白色", 5.5);
      console.log(xiaomi);
      xiaomi.call();
    </script>
  </body>
</html>
